﻿using FlyingEye.HttpServices;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FlyingEye.Views.DeviceOpsService
{
    public class PartCharacteristic
    {
        /// <summary>
        /// 特征名称，例如：温度、湿度、压力等
        /// </summary>
        public string CharacteristicName { get; set; }

        /// <summary>
        /// 特征值的数据类型
        /// </summary>
        public string DateType { get; set; }

        /// <summary>
        /// 特征值的地址
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 特征值数据长度
        /// </summary>
        public int Length { get; set; }

#pragma warning disable CS8618 // 在退出构造函数时，不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
        public PartCharacteristic()
#pragma warning restore CS8618 // 在退出构造函数时，不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
        {

        }
    }

    public class DeviceCharacteristicManagementWindowViewModel
    {
        public ObservableCollection<string> DeviceHostIdList { get; set; } = new();

        public ObservableCollection<PartCharacteristic> CharacteristicList { get; set; } = new();

        public DeviceCharacteristicManagementWindowViewModel()
        {

        }
    }

    /// <summary>
    /// DeviceCharacteristicManagementWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DeviceCharacteristicManagementWindow : Window
    {
        DeviceCharacteristicManagementWindowViewModel ViewModel = new();

        HttpClientService _httpClientService;
        HttpUrlsOptions _url;

        public DeviceCharacteristicManagementWindow(HttpClientService httpClientService,
           IOptions<HttpClientServiceOptions> options,
            IOptions<HttpUrlsOptions> httpUrlsOptions)
        {
            _url = httpUrlsOptions.Value;
            _httpClientService = httpClientService;
            options.Value.ClientId = "DeviceOpsServiceApp";
            DataContext = ViewModel;
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ViewModel.DeviceHostIdList.Add(DeviceHostIdTextBox.Text);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ViewModel.CharacteristicList.Add(new PartCharacteristic()
            {
                Address = AddressTextBox.Text,
                CharacteristicName = CharacteristicNameTextBox.Text,
                DateType = DateTypeTextBox.Text,
                Length = int.Parse(LengthTextBox.Text)
            });
        }

        class IdObject
        {
            public string Id { get; set; } = string.Empty;

            public string CharacteristicName { get; set; } = string.Empty;

            public string DevicePartName { get; set; } = string.Empty;
        }

        private async void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //var hosts = await _httpClientService.GetAsync<List<IdObject>>(_url.DeviceOpsServiceBaseUrl + "/api/device-ops-service/v1.0/device-hosts");
            foreach (var host in ViewModel.DeviceHostIdList)
            {
                var maintenances = await _httpClientService.GetAsync<List<IdObject>>(_url.DeviceOpsServiceBaseUrl
                    + "/api/device-ops-service/v1.0/device-hosts/" + host + "/device-maintenances");

                // https://localhost:44386/api/device-ops-service/v1.0/device-maintenances/3fa85f64-5717-4562-b3fc-2c963f66afa6/parts
                foreach (var m in maintenances)
                {
                    var parts = await _httpClientService.GetAsync<List<IdObject>>(_url.DeviceOpsServiceBaseUrl
                    + "/api/device-ops-service/v1.0/device-maintenances/" + m.Id + "/parts");

                    // https://localhost:44386/api/device-ops-service/v1.0/device-parts/3fa85f64-5717-4562-b3fc-2c963f66afa6/characteristics

                    var targetPart = parts.FirstOrDefault(item => item.DevicePartName == TargetPartName.Text);

                    if (targetPart == null)
                    {
                        continue;
                    }

                    var characteristics = await _httpClientService.GetAsync<List<IdObject>>(_url.DeviceOpsServiceBaseUrl
                        + "/api/device-ops-service/v1.0/device-parts/" + targetPart.Id + "/characteristics");

                    //ViewModel.CharacteristicList

                    foreach (var newc in ViewModel.CharacteristicList)
                    {
                        // 如果不存在特征点，插入特征点
                        if (!characteristics.Any(item => item.CharacteristicName == newc.CharacteristicName))
                        {
                            // https://localhost:44386/api/device-ops-service/v1.0/device-part-characteristics
                            /*
                             {
                                  "devicePartId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
                                  "characteristicName": "string",
                                  "dateType": "string",
                                  "address": "string",
                                  "length": 0
                                }
                             */
                            await _httpClientService.PostAsync<Object, Object>(_url.DeviceOpsServiceBaseUrl
                              + "/api/device-ops-service/v1.0/device-part-characteristics", new
                              {
                                  devicePartId = targetPart.Id,
                                  characteristicName = newc.CharacteristicName,
                                  dateType = newc.DateType,
                                  address = newc.Address,
                                  length = newc.Length
                              });
                        }
                    }
                }
            }

            MessageBox.Show(GetWindow(this), "特征点插入完成", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
        }
    }
}
